package com.example.filemonitor.book.service.impl;

import co.elastic.clients.elasticsearch._types.query_dsl.MatchQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;
import com.example.filemonitor.book.service.IBookSearchService;
import com.example.filemonitor.book.vo.SearchPage;
import com.example.filemonitor.book.vo.SearchVo;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;


@Service
public class BookSearchServiceImpl implements IBookSearchService {
    @Resource
    private EsClient esClient;




    @Override
    public SearchVo search(SearchPage searchPage) {

        Query byTitle = MatchQuery.of(m -> m.field("title")
                .query(searchPage.getText()))._toQuery();
        Query byContent = MatchQuery.of(m -> m.field("attachment.content")
                .query(searchPage.getText()))._toQuery();


        Query query = QueryBuilders.bool().should(byTitle,byContent).build()._toQuery(); ;


        SearchVo searchVo = esClient.getPage(query,searchPage.getPager(), searchPage.getSize());


        // 返回VO
        return searchVo;
    }


    @Override
    public SearchVo searchOneById(SearchPage searchPage) {
        Query byId = MatchQuery.of(m -> m.field("id")
                .query(searchPage.getText()))._toQuery();
        Query query = QueryBuilders.bool().must(byId).build()._toQuery(); ;


        SearchVo searchVo = esClient.getPage(query,searchPage.getPager(), searchPage.getSize());


        // 返回VO
        return searchVo;
    }
}
